from services.base_service import BaseService
from models.resource_model import ResourceModel
from models.employee_model import EmployeeModel
from cache import cache


class ResourceService(BaseService):
    def __init__(self, *args, **kwargs):
        super(ResourceService, self).__init__(*args, **kwargs)

    def create_resource(self, video, video_url, cover_pic, title, info, category, director_id, resource_type):
        resource = ResourceModel(
            video=video,
            video_url=video_url,
            cover_pic=cover_pic,
            title=title,
            info=info,
            category=category,
            director_id=director_id,
            resource_type=resource_type
        )
        with self.session_scope() as session:
            session.add(resource)
            session.commit()
            cache.delete_memoized(ResourceService.resource)
            cache.delete_memoized(ResourceService.resources)
        return True

    @cache.memoize(timeout=300, args_to_ignore=['self'], make_name="resources")
    def resources(self, director_id=None):
        with self.session_scope() as session:
            query = session.query(ResourceModel, EmployeeModel).outerjoin(EmployeeModel, EmployeeModel.id == ResourceModel.director_id)
            if director_id:
                query = query.filter(ResourceModel.director_id == director_id)
            query = query.all()
            resourcesList = []
            for result in query:
                resourceModel = result[0]
                employeeModel = result[1] if result[1] else EmployeeModel()

                employee = {
                    "id": employeeModel.id,
                    "name": employeeModel.name,
                    "avatar": employeeModel.avatar,
                    "title1": employeeModel.title1,
                    "title2": employeeModel.title2,
                    "index": employeeModel.index,
                    "production": employeeModel.production
                }

                resource = {
                    "id": resourceModel.id,
                    "created_at": str(resourceModel.created_at),
                    "video": resourceModel.video,
                    "video_url": resourceModel.video_url,
                    "cover_pic": resourceModel.cover_pic,
                    "title": resourceModel.title,
                    "info": resourceModel.info,
                    "category": resourceModel.category,
                    "resource_type": resourceModel.resource_type,
                    "director": employee
                }

                resourcesList.append(resource)

        return resourcesList

    @cache.memoize(timeout=300, args_to_ignore=['self'], make_name="resource")
    def resource(self, resource_id):
        with self.session_scope() as session:
            query = session.query(ResourceModel, EmployeeModel).outerjoin(EmployeeModel,
                                                                          EmployeeModel.id == ResourceModel.director_id).filter(
                ResourceModel.id == int(resource_id)).first()
            if not query:
                self.return_error(20002)

            resourceModel = query[0]
            employeeModel = query[1] if query[1] else EmployeeModel()

            employee = {
                "id": employeeModel.id,
                "name": employeeModel.name,
                "avatar": employeeModel.avatar,
                "title1": employeeModel.title1,
                "title2": employeeModel.title2,
                "index": employeeModel.index,
                "production": employeeModel.production
            }

            resource = {
                "id": resourceModel.id,
                "created_at": str(resourceModel.created_at),
                "video": resourceModel.video,
                "video_url": resourceModel.video_url,
                "cover_pic": resourceModel.cover_pic,
                "title": resourceModel.title,
                "info": resourceModel.info,
                "category": resourceModel.category,
                "resource_type": resourceModel.resource_type,
                "director": employee
            }

        return resource

    def update_resource(self, resource_id, video, video_url, cover_pic, title, info, category, director_id, resource_type):
        with self.session_scope() as session:
            resource = session.query(ResourceModel) \
                .filter(ResourceModel.id == resource_id).first()

            if not resource:
                self.return_error(20002)

            resource.video = video
            resource.video_url = video_url,
            resource.cover_pic = cover_pic
            resource.title = title
            resource.category = category
            resource.director_id = director_id
            resource.info = info
            resource.resource_type = resource_type
            session.commit()
            cache.delete_memoized(ResourceService.resource)
            cache.delete_memoized(ResourceService.resources)
        return True

    def remove_resource(self, resource_id):
        with self.session_scope() as session:
            resource = session.query(ResourceModel) \
                .filter(ResourceModel.id == resource_id).first()
            if not resource:
                self.return_error(20002)
            session.delete(resource)
            session.commit()
            cache.delete_memoized(ResourceService.resource)
            cache.delete_memoized(ResourceService.resources)
        return True
